package Model;

import java.io.Serializable;

import Messaging.Notification;
import Messaging.NotificationManager;

/*
 * @author Paolo Cordova
 */
public class PlayerInJail extends PlayerState implements Serializable
{

	private static PlayerInJail INSTANCE = null;
	final static long serialVersionUID = 211;
	
	protected PlayerInJail() 
	{

	}
	
	public static PlayerState Instance() 
	{
		if (INSTANCE == null) {
			INSTANCE = new PlayerInJail();
		}
		
		return INSTANCE;
	}

	@Override
	public void movePiece(Player player, int spaces) 
	{
	
	}
	
	public void movePiece(Player player, Dice dice)
	{
		int i = player.getNumOfRolls();
		player.setNumOfRolls(++i);
		
		if ((dice.getDie1() == dice.getDie2()) || (i > 2)) 
		{	
			NotificationManager.getInstance().notifyObservers(Notification.ACTION_MESSAGE, player.getName() + " has been set free!");
			
			getOutOfJail(player);
			player.updatePosition(dice.getTotalRoll());
			player.setNumOfRolls(0);
		}
		
		if (i == 3)
			player.updateCash(-50);
	}
	
	@Override
	public void collectRent(Player payee, int amount, Player payer) 
	{
		if (payer.getIndex() != payee.getIndex()) {
			payee.updateCash(amount);
			payer.updateCash(-amount);
			NotificationManager.getInstance().notifyObservers(Notification.ACTION_MESSAGE, payer.getName() + " paid $" + amount + " in rent to " + payee.getName());
		}
	}
	
	@Override
	public void goToJail(Player player)
	{
	
	}
	
	@Override
	public void getOutOfJail(Player player) 
	{
		player.changeState(PlayerFree.Instance());
	}
	
	public String toString() 
	{
		return "PlayerInJail";
	}

}
